//
//  UIView+DCHView.m
//  FareWell_Obj_C
//
//  Created by 丁诚昊 on 16/1/5.
//  Copyright © 2016年 iOS.ZSTech.丁诚昊(831). All rights reserved.
//

#import "UIView+Geometry.h"
#import "UIView+PhyMoVal.h"
#import "UIColor+ColorFullTemplate.h"

@implementation UIView (DCHView)
#pragma mark -- Frame & Geopraphy 坐标系快捷方式设置
-(void)setGeoTop:(CGFloat)GeoTop{CGRect frame=self.frame;frame.origin.y= GeoTop;self.frame = frame;}-(void)setGeoLeft:(CGFloat)GeoLeft{CGRect frame = self.frame;frame.origin.x = GeoLeft;self.frame = frame;}-(void)setGeoCenterY:(CGFloat)GeoCenterY{CGPoint oriCenter = self.center;oriCenter.y = GeoCenterY;oriCenter.x = self.center.x;self.center = oriCenter;}-(void)setGeoCenterX:(CGFloat)GeoCenterX{CGPoint oriCenter = self.center;oriCenter.x = GeoCenterX;self.center = oriCenter;}-(void)setGeoWidth:(CGFloat)GeoWidth{CGRect frame = self.frame;frame.size.width = GeoWidth;self.frame = frame;}-(void)setGeoHeight:(CGFloat)GeoHeight{CGRect frame = self.frame;frame.size.height = GeoHeight;self.frame= frame;}-(void)setGeoSize:(CGSize)GeoSize{CGRect frame = self.frame;frame.size = GeoSize;self.frame = frame;}-(CGFloat)GeoTop{return self.frame.origin.y;}-(CGFloat)GeoLeft{return self.frame.origin.x;}-(CGFloat)GeoCenterX{return self.center.x;}-(CGFloat)GeoCenterY{return self.center.y;}-(CGFloat)GeoWidth{return self.frame.size.width;}-(CGFloat)GeoHeight{return self.frame.size.height;}-(CGSize)GeoSize{return self.frame.size;}

/**
 * @param 从文件读取一个storyboard文件并渲染一个UIView
 */
+(UIView*) loadStroyboardfromIO:(NSString*)baseUrl
{
    // NSString *storyboardName = baseUrl ; //无后缀.storyboard
    return [[UIStoryboard  storyboardWithName:baseUrl bundle:nil]instantiateInitialViewController ].view;
}

+ (void*) Rendering_setBgImg2UIView:(UIView*)view with:(NSString*)imageUrl;
{
    
    NSArray<NSString*>* subs = [imageUrl componentsSeparatedByString:@"."];
    NSLog(@"直接设置UIView的背景图片 = %@",subs);
    
    if (subs.count >= 1)
    {
        NSString* resPath = [[NSBundle mainBundle]
                             pathForResource:[subs firstObject]
                             ofType:[subs lastObject]];
        
        UIImage *realImage = [UIImage imageWithContentsOfFile:resPath];
        
        //rendering the UIView with UIImages in the pathUrl .[当前路径下的最后一张图片...]
        view.layer.contents = (id)realImage.CGImage;
    }
    else
    {
        view.layer.contents = (id)[[UIImage imageNamed:imageUrl] CGImage];
    }
    
    return (__bridge void *)(view);
}

#pragma mark - UIView & 简单渲染UIView
/**
 阴影的位置和宽度 附着到View背后 默认为浅灰色 拟态：太阳
 @param 一个View指针引用
 @return 返回一个引用
 */

+ (void*) Rendering_ShadowView:(UIView*)view strongLevel:(shadonwLevel)level cornerRadius:(CGFloat)value
{
    CGFloat shadowValue = view.bounds.size.width / ((NSInteger)(level*2)) ;/**早中晚*/
    
    CGSize shadownSize;
    if(level + 1 >= 1){
         shadownSize= CGSizeMake(shadowValue * (level + 1)/10 , shadowValue / 4);
    }else{
        shadownSize = CGSizeMake((shadowValue * (1 - level ))/10 , -shadowValue / 4);
    }
    view.layer.shadowOffset = shadownSize ;
    view.layer.shadowColor = [UIColor grayColor].CGColor;
    view.layer.shadowOpacity = level*level/10;
    view.layer.shadowRadius = shadowValue/(level*2);
    
    CGFloat realCornerRadius;
    if(value <= 0.0){
        realCornerRadius = view.frame.size.height / 2;
    }else{
        realCornerRadius = value;
    }

    //view.layer.cornerRadius = shadowValue/5 > 10 ? 5 : shadowValue / 10;
    return (__bridge void*)view;
}

#pragma mark - UIView & 简单渲染UIView
/**
 阴影的位置和宽度 附着到View背后 默认为浅灰色 拟态：太阳
 @param 一个View指针引用
 @return 返回一个引用
 */
+ (void*) Rendering_ShadowView:(UIView*)view strongLevel:(shadonwLevel)level
{
    switch (level) {
        case shadonwLevelStrong:
            break;
        case shadonwLevelMiddle:
            break;
        case shadonwLevelMorning:
            break;
        case shadonwLevelEvening:
            break;
            
        default:
            break;
    }
    
    CGFloat shadowValue = view.bounds.size.width / ( level  * 10 );
    CGFloat shadownOffsetW,shadownOffsetH;
    shadownOffsetW = shadownOffsetH = shadowValue * ((level%2==0)?-1:1);
    view.layer.shadowOffset = CGSizeMake(shadownOffsetW,shadownOffsetW);
    view.layer.shadowRadius = view.GeoWidth / level / 5 ;
    
    view.layer.shadowOpacity = pow(level,3);
    view.layer.shadowColor = [UIColor grayColor].CGColor;
    
    return (__bridge void*)view;
}

/**
 * 递归接受一个View 并渲染内部的平层所有View 添加阴影效果
 * @param String ---> 必须指定View的ClassName名称 否则不进行渲染....
 */
+ (void*) Rendering_ShadowView_Recursively:(UIView*)oriView ClassName:(Class)clsName
{
    
    [self Rendering_ShadowView:oriView strongLevel:shadonwLevelStrong cornerRadius:2.5f];
    [oriView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop){
        if ([obj isKindOfClass:clsName]){
            [self Rendering_ShadowView:obj strongLevel:shadonwLevelMiddle cornerRadius:1.2f];
        }
    }];
    return (__bridge void*)oriView;
}

+ (UIBarButtonItem*) _createItemView:(UIImage*)imageName
                         withHLImage:(UIImage*)HLImage
                              target:(id)target
                           actionSel:(SEL)selector {
    UIButton* buttonItem = [[UIButton alloc] init];
    [buttonItem setBackgroundImage:imageName forState:UIControlStateNormal];
    [buttonItem setBackgroundImage:HLImage forState:UIControlStateHighlighted];
    buttonItem.GeoSize = buttonItem.currentBackgroundImage.size;
    ///绑定按钮事件
    [buttonItem addTarget:target
                   action:selector
         forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:buttonItem];
}
@end

@implementation UIView (shadowViewInEnglend)
+(void) statusBatteryHSBView:(UIView*)superView WithTag:(long)tag{
    /// 顶部20像素阴影控件
    UIView *temp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kscreenW, 20)];
    temp.tag = tag;
    temp.GeoCenterX = superView.GeoCenterX;
    temp.backgroundColor = [UIColor Heavy_ColorfulDay];
    temp.alpha = 0.0f;
    
    [UIView Rendering_ShadowView:temp strongLevel:shadonwLevelEvening cornerRadius:12];
    [UIView animateWithDuration:3.33f animations:^{
        [superView addSubview:temp];
        temp.alpha = 1.0f;
    }completion:^(BOOL finished) {
        [superView layoutIfNeeded];
        [superView setNeedsDisplay];
    }];
}

/**
 * 给一个覆盖的毛玻璃
 */
+(UIView*) DCH_addBlurView :(CGRect)frame blurType:(UIBlurEffectStyle)blurType {
    
    UIVisualEffect* effest = [UIBlurEffect effectWithStyle:blurType];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:effest];
    blurView.frame = frame;
    blurView.clipsToBounds = true;

    return blurView;
}

+(void) simplifyWithCornerRadius:(UIView*)container with:(CGFloat)cornerRadius{
    container.layer.cornerRadius = cornerRadius;
    container.clipsToBounds = container.layer.cornerRadius > 0.0;
    return ;
}

+ (CGRect) StringRectWithFont:(UIFont*)font
                         type:(RectPramsEnum)enumType
                   ParamValue:(CGFloat)value
                     InString:(NSString*)oriStr{
    CGSize preSize = CGSizeMake(44, 44);
    if (enumType == RectPramsEnumWidth) {
        preSize = CGSizeMake(value, CGFLOAT_MAX);
    }
    if (enumType == RectPramsEnumHeight) {
        preSize = CGSizeMake(CGFLOAT_MAX, value);
    }
    
    NSDictionary* KVP = @{NSFontAttributeName:font};
    CGRect framer = [oriStr boundingRectWithSize:preSize
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:KVP
                                             context:nil];
    return framer;
}

@end





